if TUNING.NPC_UPGRADE_ITEMS_FN == nil then
    TUNING.NPC_UPGRADE_ITEMS_FN = {}
end


local function Fn()
    local Fx = Class()

    
    local function GetStringsTable()
        return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["nightsword_upgrade"] or {}
    end
    function Fx:Init(inst)        
    end
    function Fx:upgrade_bloody(inst)

        inst:RemoveComponent("shadowlevel") 
    
        inst:RemoveTag("shadow_item")
        inst:RemoveTag("shadow")
        inst:RemoveTag("sharp")
        inst:RemoveTag("shadowlevel")
    
        inst.components.named:SetName(GetStringsTable().name_blood)
    
        inst:ListenForEvent("rechargechange",function(inst,_table)
            if _table.percent == 1 then
                inst.components.weapon:SetDamage(0) ----- 攻击力 为 0
                inst.components.weapon:SetRange(25) ----- 距离增加
                inst.__range_flag = false
            else
                if inst.__range_flag ~= true then
                    inst.__range_flag = true
                    inst.components.weapon:SetDamage(TUNING.NIGHTSWORD_DAMAGE)
                    inst.components.weapon:SetRange(1)
                end
            end
        end)
        inst.components.weapon:SetDamage(0) ----- 攻击力 为 0
        inst.components.weapon:SetRange(25) ----- 距离增加
    
        inst.components.equippable.dapperness = 0   --- 穿上后造成的精神值降低
        inst.components.equippable.is_magic_dapperness = true  ---- 必须留下这个，不然很多目标不能主动攻击
        --------------------------------------------------------------------------
        ----- 冷却相关
        -- inst:AddComponent("rechargeable") --- 移出到武器外面，不在 DoTaskInTime 里加载
        local cool_down_time = 5
        inst.components.rechargeable:SetMaxCharge(cool_down_time)
        --------------------------------------------------------------------------
        --- 耐久度相关修改
        local finiteuses_use = inst.components.finiteuses.total / 80    --- 80下
    
        -- inst.components.finiteuses.Use_2 = inst.components.finiteuses.Use    ---- 用这个方法会出现奇特的情况
        inst.components.finiteuses.Use = function(self,num,flag)
            if flag == true then
                self:SetUses(self.current - (num or 1))    
            end
        end
        --------------------------------------------------------------------------
    
        local color = Vector3(130,0,0)
        --------------------------------------------------------------------------
    
        local function Aoe_Tar_PT_with_ban(tar_pt,ban_list)
            if ban_list == nil then
                ban_list = {}
            end
            local canthavetags = { "companion","isdead","player","INLIMBO", "notarget", "noattack", "flight", "invisible", "playerghost" ,"chester","hutch","wall","structure"}
            local musthavetags = nil
            local musthaveoneoftags = {"pig","rabbit","animal","smallcreature","epic","monster","insect"}
            local ents = TheSim:FindEntities(tar_pt.x, 0, tar_pt.z, 5.5, musthavetags, canthavetags, musthaveoneoftags)
            for k, monster in pairs(ents) do
                if monster and monster:IsValid() and ban_list[monster] ~= true then
                    if monster.components.combat then
                        monster.components.combat:GetAttacked(monster:GetNearestPlayer(),math.random(30,50))
                    elseif monster.components.health then
                        monster.components.health:DoDelta(-math.random(30,50))
                    end
                    SpawnPrefab("npc_fx_shadow_feather"):PushEvent("Set",{
                        pt = Vector3(monster.Transform:GetWorldPosition()) ,
                        scale = Vector3(0.5,0.5,0.5),
                        color = color,
                    })
                end
            end
        end
        --------------------------------------------------------------------------
    
    
        local function target_fn(preojectile,attacker,target) --- 触碰到目标后执行
            local pt = Vector3(preojectile.Transform:GetWorldPosition())
            local tar_pt = pt
            if target and target.Transform then
                tar_pt = Vector3( target.Transform:GetWorldPosition() )
            end
            --------------------------------------------------------------------------
            local temp_list = {}
            if target and target:IsValid() then -------- 如果目标还存在
                if target.components.combat then
                    target.components.combat:GetAttacked(target:GetNearestPlayer(),math.random(60,80))
                elseif target.components.health then
                    target.components.health:DoDelta(-math.random(60,80))
                end
                temp_list[target] = true
            end
            SpawnPrefab("npc_fx_shadow_feather"):PushEvent("Set",{
                pt = tar_pt,
                scale = Vector3(0.7,0.7,0.7),
                color = color,
            })
            ------------- AOE
            Aoe_Tar_PT_with_ban(tar_pt,temp_list)
            --------------------------------------------------------------------------
            ------------- 反伤玩家一点血            
                SpawnPrefab(preojectile.prefab):PushEvent("Set",{
                    pt = tar_pt,
                    target = attacker,
                    owner = attacker,
                    range = 1.5,
                    speed = 12,
                    color = color,
                    target_fn = function(preojectile,attacker,target)
                        if attacker and attacker:IsValid() and attacker:HasTag("player") and not attacker:HasTag("playerghost") and attacker.components.health and attacker.components.combat then
                            -- attacker.components.health:DoDelta(-1)
                            attacker.components.combat:GetAttacked(nil,3)
                            SpawnPrefab("npc_fx_shadow_feather"):PushEvent("Set",{
                                pt = Vector3(attacker.Transform:GetWorldPosition()),
                                scale = Vector3(0.4,0.4,0.4),
                                color = color,
                            })
                            Aoe_Tar_PT_with_ban(Vector3(attacker.Transform:GetWorldPosition()))
                        end
                    end,
                })
            --------------------------------------------------------------------------
            --------------------------------------------------------------------------
        end
        local function OnAttack(inst,attacker,target)
            if not attacker:HasTag("player") then
                return
            end
            if not inst.components.rechargeable:IsCharged() then
                return
            end
            inst.components.rechargeable:Discharge(cool_down_time)
    
            inst.components.finiteuses:Use(finiteuses_use,true)
    
            local points = inst.npc_base_lib:GetSurroundPoints({
                range = 2,
                num = 3
            })
            SpawnPrefab("npc_projectile_shadow_rook"):PushEvent("Set",{
                pt = points[1],
                target = target,
                owner = attacker,
                range = 1.5,
                target_fn = target_fn,
                color = color,
            })
            SpawnPrefab("npc_projectile_shadow_knight"):PushEvent("Set",{
                pt = points[2],
                target = target,
                range = 1.5,
                owner = attacker,
                target_fn = target_fn,
                color = color,
            })
            SpawnPrefab("npc_projectile_shadow_bishop"):PushEvent("Set",{
                pt = points[3],
                target = target,
                range = 1.5,
                owner = attacker,
                target_fn = target_fn,
                color = color,
            })
    
        end
        inst.components.weapon:SetOnAttack(OnAttack)
        --------------------------------------------------------------------------
    
    
        --------------------------------------------------------------------------
    end

    return Fx
end


if TUNING.NPC_UPGRADE_ITEMS_FN["nightsword"] == nil then    
    TUNING.NPC_UPGRADE_ITEMS_FN["nightsword"] = Fn
end
